package com.sunday.ble.orlant.adapter;

import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.sunday.ble.orlant.R;
import com.sunday.ble.orlant.greendao.Device;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Created by littlexs on 2017/6/15.
 */

public class BlueToothAdapter extends BaseAdapter {

    private Context context;
    private List<BluetoothDevice> bluetoothDeviceList = new ArrayList<>();
    private List<Device> adapterShowList;

    public BlueToothAdapter(Context context, List<BluetoothDevice> bluetoothDeviceList, List<Device> adapterShowList){
        this.context = context;
        this.bluetoothDeviceList = bluetoothDeviceList;
        this.adapterShowList = adapterShowList;
    }

    @Override
    public int getCount() {
        return bluetoothDeviceList.size();
    }

    @Override
    public Object getItem(int position) {
        return bluetoothDeviceList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (null == convertView) {
            viewHolder = new ViewHolder();
            LayoutInflater mInflater = LayoutInflater.from(context);
            convertView = mInflater.inflate(R.layout.blue_item, null);
            viewHolder.name = (TextView) convertView
                    .findViewById(R.id.name);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        if (!adapterShowList.isEmpty()){
            Device  device1 = adapterShowList.get(position);
            viewHolder.name.setText(device1.getName());
        }
        return convertView;
    }

    private OnItemListener listener;

    public interface OnItemListener{
        void itemClick(int position,BluetoothDevice device);
    }

    public void setOnItemListener(OnItemListener listener){
        this.listener = listener;
    }

    private class ViewHolder {
        TextView name;
    }
}
